package com.cq.xjms.service.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.extremecomponents.table.limit.Limit;

import com.cq.xjms.model.Teacher;
import com.cq.xjms.service.TeacherManager;

public class TeacherManagerImpl extends BaseManagerImpl<Teacher> implements TeacherManager{
	private static Logger LOG = Logger.getLogger(TeacherManagerImpl.class);
	
	public List<Teacher> queryTeachers(Map<String, Object> params,Limit limit) {
		String hql = generateHql(params);
		LOG.info(hql);
		List<Teacher> teachers = null;
		try{
			teachers = this.findByHQL(hql, null, limit);
		}catch(Exception e){
			LOG.error("query user", e);
		}
		return teachers;
	}
	
	

	@Override
	public int countTeacher(Map<String, Object> params) {
		return this.countByHQL(generateHqlByCount(params), null);
	}

	private String generateHql(Map<String, Object> map) {
		StringBuffer hql = new StringBuffer();
		hql.append("select t from Teacher t ");
		if (map.size() > 0) {
			hql.append(" where 1=1 ");
			Iterator it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = (String) it.next();
				hql.append(" and t." + key + "=" + map.get(key));
			}
		}
		hql.append(" order by t.createDate desc");
		return hql.toString();
	}
	private String generateHqlByCount(Map<String, Object> map) {
		StringBuffer hql = new StringBuffer();
		hql.append("select count(t) from Teacher t ");
		if (map.size() > 0) {
			hql.append(" where 1=1 ");
			Iterator it = map.keySet().iterator();
			while (it.hasNext()) {
				String key = (String) it.next();
				hql.append(" and t." + key + "=" + map.get(key));
			}
		}
		return hql.toString();
	}
	
	
}
